home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / sounddt41src / library.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  8KB  |  322 lines

  1.  
  2. /*
  3.  *  SoundDT41 -- A free release of the sound.datatype v41 source code
  4.  *  Copyright (C) 1998/99  Stephan Rupprecht
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  */
  21.  
  22. #include <exec/resident.h>
  23. #include <exec/initializers.h>
  24. #include <exec/execbase.h>
  25. #include <exec/libraries.h>
  26. #include <datatypes/soundclass.h>
  27.  
  28. #ifdef __MAXON__
  29. #include <pragma/exec_lib.h>
  30. #include <pragma/intuition_lib.h>
  31. #include <pragma/dos_lib.h>
  32. #include <pragma/utility_lib.h>
  33. #include <pragma/datatypes_lib.h>
  34. #else
  35. #include <inline/exec.h>
  36. #include <inline/intuition.h>
  37. #include <inline/dos.h>
  38. #include <inline/utility.h>
  39. #include <inline/datatypes.h>
  40. #endif
  41.  
  42. #include "classbase.h"
  43.  
  44. #ifndef __MAXON__
  45. #include "CompilerSpecific.h"
  46. #endif
  47.  
  48. /****************************************************************************/
  49.  
  50. #define BPTR        ULONG
  51.  
  52. #ifdef __MAXON__
  53. #define REG(r, a)        register __## r a
  54. #define __regargs
  55. #endif
  56.  
  57. #define VERSION        41
  58. #define REVISION        8
  59. #define VERSIONSTR    "41.8"
  60. #define DATE            "29.12.98"
  61.  
  62. /****************************************************************************/
  63.  
  64. STATIC struct Library *LibInit(REG(a0, ULONG Segment), REG(d0, struct ClassBase *ClassBase), REG(a6, struct Library *) );
  65. STATIC struct Library *LibOpen( REG(a6, struct ClassBase *ClassBase) );
  66. STATIC ULONG LibExpunge( REG(a6, struct ClassBase *ClassBase) );
  67. STATIC ULONG LibClose( REG(a6, struct ClassBase *ClassBase) );
  68. STATIC LONG LibExtFunc(void);
  69.  
  70. BOOL __regargs L_OpenLibs(struct ClassBase *);
  71. void __regargs L_CloseLibs(struct ClassBase *);
  72. Class * __regargs initClass (struct ClassBase *);
  73. Class  * ObtainClass( REG(a6, struct ClassBase *) );
  74.  
  75. extern ULONG   Dispatch( REG(a0, Class *), REG(a2, Object *), REG(a1, Msg) );
  76.  
  77. /****************************************************************************/
  78.  
  79. UBYTE    LibName[] = SOUNDDTCLASS,
  80.         LibID[] = "sound.datatype "VERSIONSTR" ("DATE") © 1998/99 by Stephan Rupprecht";
  81.  
  82. LONG    LibVersion = VERSION,
  83.         LibRevision = REVISION;
  84.  
  85. APTR LibVectors[] =
  86. {
  87.     (APTR) LibOpen,
  88.     (APTR) LibClose,
  89.     (APTR) LibExpunge,
  90.     (APTR) LibExtFunc,
  91.     (APTR) ObtainClass,
  92.     (APTR) -1L
  93. };
  94.  
  95. ULONG LibInitTab[] =
  96. {
  97.     (ULONG) sizeof(struct ClassBase),
  98.     (ULONG) LibVectors,
  99.     (ULONG) NULL,
  100.     (ULONG) LibInit
  101. };
  102.  
  103. struct Resident ROMTag =     /* do not change */
  104. {
  105.     RTC_MATCHWORD,
  106.      &ROMTag,
  107.      &ROMTag + 1L,
  108.     RTF_AUTOINIT,
  109.      VERSION,
  110.     NT_LIBRARY,
  111.      0,
  112.     LibName,
  113.     LibID,
  114.     (APTR) LibInitTab
  115. };
  116.  
  117. /****************************************************************************/
  118.  
  119. LONG dummy(void)
  120. {
  121.     return(-1L);
  122. }
  123.  
  124. /****************************************************************************/
  125.  
  126. LONG LibExtFunc(void)
  127. {
  128.     return(0L);
  129. }
  130.  
  131. /****************************************************************************/
  132.  
  133. #ifndef __MAXON__
  134. #define IntuitionBase    cb->cb_IntuitionBase
  135. #define GfxBase        cb->cb_GfxBase
  136. #define DOSBase        cb->cb_DOSBase
  137. #define SysBase        cb->cb_SysBase
  138. #define DataTypesBase    cb->cb_DataTypesBase
  139. #define UtilityBase        cb->cb_UtilityBase
  140. #define IFFParseBase    cb->cb_IFFParseBase
  141. #define TapeDeckBase    cb->cb_TapeDeckBase
  142. #else
  143. struct Library        *DOSBase, *SysBase, *DataTypesBase, *TapeDeckBase,
  144.                 *IntuitionBase, *GfxBase, *UtilityBase, *IFFParseBase;
  145. #endif
  146.  
  147. /****************************************************************************/
  148.  
  149. struct Library *
  150.   LibInit( REG(a0, ULONG Segment), REG(d0, struct ClassBase *cb), REG(a6, struct Library *ExecBase) )
  151. {
  152.     SysBase = ExecBase;
  153.  
  154.     InitSemaphore( &cb->cb_LibLock );
  155.     ObtainSemaphore( &cb->cb_LibLock );
  156.     
  157.     cb->LibSegment = Segment;
  158.     cb->LibNode.lib_Revision = REVISION;
  159.     
  160.     if( ((struct ExecBase *)ExecBase)->AttnFlags & AFF_68020 )
  161.     {
  162.         if(L_OpenLibs(cb))
  163.         {    
  164.             if(cb->cb_Class = initClass(cb))
  165.             {    
  166.                 ReleaseSemaphore( &cb->cb_LibLock );
  167.                 return((struct Library *)cb);
  168.             } 
  169.         }
  170.     }
  171.  
  172.     L_CloseLibs( cb );
  173.  
  174.     ReleaseSemaphore( &cb->cb_LibLock );
  175.  
  176.     FreeMem((BYTE *)cb-cb->LibNode.lib_NegSize, 
  177.         cb->LibNode.lib_NegSize + cb->LibNode.lib_PosSize);
  178.     
  179.     return(NULL);
  180. }
  181.  
  182. /****************************************************************************/
  183.  
  184. struct Library *LibOpen( REG(a6, struct ClassBase *cb) )
  185. {    
  186.     ObtainSemaphore( &cb->cb_LibLock );
  187.     cb->LibNode.lib_Flags &= ~LIBF_DELEXP;
  188.     cb->LibNode.lib_OpenCnt++;
  189.     ReleaseSemaphore( &cb->cb_LibLock );
  190.     return((struct Library *)cb);
  191. }
  192.  
  193. /****************************************************************************/
  194.  
  195. ULONG  LibExpunge( REG(a6, struct ClassBase *cb) )
  196. {
  197.     ULONG    retval = 0L;
  198.     
  199.     ObtainSemaphore( &cb->cb_LibLock );
  200.     
  201.      if(!cb->LibNode.lib_OpenCnt) 
  202.      {
  203.         if(cb->cb_Class) 
  204.         {
  205.             if( ! FreeClass(cb->cb_Class) )
  206.             {
  207.                 AddClass( cb->cb_Class );
  208.                 ReleaseSemaphore( &cb->cb_LibLock );
  209.                 return( NULL );
  210.             }
  211.         }
  212. #ifdef __GNUC__
  213.         if( DataTypesBase->lib_Version >= 45L )
  214.         {
  215.             FreeDTMethods( cb->cb_Methods );
  216.         }
  217. #else
  218.         FreeVec( cb->cb_Methods );
  219. #endif
  220.         
  221.         L_CloseLibs(cb);
  222.         
  223.          Remove((struct Node *)cb);
  224.  
  225.           FreeMem((BYTE *)cb-cb->LibNode.lib_NegSize,
  226.               cb->LibNode.lib_NegSize + cb->LibNode.lib_PosSize);
  227.  
  228.         retval = cb->LibSegment;
  229.      } 
  230.      else 
  231.     {
  232.         cb->LibNode.lib_Flags |= LIBF_DELEXP;
  233.     }
  234.  
  235.     ReleaseSemaphore( &cb->cb_LibLock );
  236.  
  237.      return( retval );
  238. }
  239.  
  240. /****************************************************************************/
  241.  
  242. ULONG  LibClose( REG(a6, struct ClassBase *cb) )
  243. {    
  244.     ULONG    retval = 0L;
  245.     
  246.     ObtainSemaphore( &cb->cb_LibLock );
  247.     
  248.      if( ! cb->LibNode.lib_OpenCnt || ! --cb->LibNode.lib_OpenCnt )
  249.     {
  250.         if( cb->LibNode.lib_Flags & LIBF_DELEXP )
  251.         {
  252.             retval = LibExpunge( cb );
  253.         }
  254.     }
  255.  
  256.     ReleaseSemaphore( &cb->cb_LibLock );
  257.     
  258.     return( retval );
  259. }
  260.  
  261. /****************************************************************************/
  262.  
  263. BOOL __regargs L_OpenLibs( struct ClassBase *cb )
  264. {
  265.     if(    ( DOSBase = OpenLibrary( "dos.library", 0L ) ) &&
  266.         ( IntuitionBase = OpenLibrary( "intuition.library", 0L ) ) &&
  267.         ( GfxBase = OpenLibrary( "graphics.library", 0L ) ) &&
  268.         ( DataTypesBase = OpenLibrary( "datatypes.library", 0L ) ) &&
  269.         ( UtilityBase = OpenLibrary( "utility.library", 0L ) ) &&
  270.         ( IFFParseBase = OpenLibrary( "iffparse.library", 0L ) )    )
  271.     {    
  272.         TapeDeckBase = OpenLibrary( "gadgets/tapedeck.gadget", 39L );
  273.         return TRUE;
  274.     }
  275.     
  276.     return FALSE;
  277. }
  278.  
  279. /****************************************************************************/
  280.  
  281. Class * __regargs initClass ( struct ClassBase *cb )
  282. {
  283.      register Class *cl;
  284.  
  285.      if(cl = MakeClass(LibName, DATATYPESCLASS, NULL, sizeof( struct InstanceData ), 0L))
  286.       {
  287.            cl->cl_Dispatcher.h_Entry = (HOOKFUNC) Dispatch;
  288.            cl->cl_UserData = (ULONG) cb;
  289.            AddClass(cl);
  290.       }
  291.  
  292.      return (cl);
  293. }
  294.  
  295. /****************************************************************************/
  296.  
  297. void __regargs L_CloseLibs( struct ClassBase *cb )
  298. {
  299.     CloseLibrary( DOSBase );
  300.     CloseLibrary( IntuitionBase );
  301.     CloseLibrary( GfxBase );
  302.     CloseLibrary( DataTypesBase );
  303.     CloseLibrary( UtilityBase );
  304.     CloseLibrary( IFFParseBase );
  305.     CloseLibrary( TapeDeckBase );
  306. }
  307.  
  308. /****************************************************************************/
  309.  
  310. Class *ObtainClass( REG(a6, struct ClassBase *cb) )
  311. {
  312.     Class    *cl;
  313.     
  314.     ObtainSemaphoreShared( &cb->cb_LibLock );
  315.     cl = cb->cb_Class;
  316.     ReleaseSemaphore( &cb->cb_LibLock );
  317.     
  318.     return cl;
  319. }
  320.  
  321. /****************************************************************************/
  322.